package com.ys.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ys.common.utils.PageUtils;
import com.ys.common.utils.Query;
import com.ys.gulimall.product.dao.BrandDao;
import com.ys.gulimall.product.dao.CategoryBrandRelationDao;
import com.ys.gulimall.product.dao.CategoryDao;
import com.ys.gulimall.product.entity.BrandEntity;
import com.ys.gulimall.product.entity.CategoryBrandRelationEntity;
import com.ys.gulimall.product.entity.CategoryEntity;
import com.ys.gulimall.product.service.BrandService;
import com.ys.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
	
	@Resource
	private BrandDao brandDao;
	
	@Resource
	private CategoryDao categoryDao;
	
	@Resource
	private CategoryBrandRelationDao relationDao;
	
	@Resource
	private BrandService brandService;
	
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<CategoryBrandRelationEntity> page = this.page(
				new Query<CategoryBrandRelationEntity>().getPage(params),
				new QueryWrapper<CategoryBrandRelationEntity>()
		);
		
		return new PageUtils(page);
	}
	
	@Override
	public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
		Long brandId = categoryBrandRelation.getBrandId();
		Long catelogId = categoryBrandRelation.getCatelogId();
		
		//1、查询品牌详细信息
		BrandEntity brandEntity = brandDao.selectById(brandId);
		//2、查询分类详细信息
		CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
		
		//将信息保存到categoryBrandRelation中
		categoryBrandRelation.setBrandName(brandEntity.getName());
		categoryBrandRelation.setCatelogName(categoryEntity.getName());
		
		// 保存到数据库中
		this.baseMapper.insert(categoryBrandRelation);
	}
	
	@Override
	public void updateBrand(Long brandId, String name) {
		CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
		relationEntity.setBrandId(brandId);
		relationEntity.setBrandName(name);
		this.baseMapper.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
	}
	
	@Override
	public void updateCategory(Long catId, String name) {
		this.baseMapper.updateCategory(catId, name);
	}
	
	@Override
	public List<BrandEntity> getBrandsByCatId(Long catId) {
		
		List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
		
		List<BrandEntity> collect = catelogId.stream().map(item -> {
			Long brandId = item.getBrandId();
			//查询品牌的详情
			BrandEntity byId = brandService.getById(brandId);
			return byId;
		}).collect(Collectors.toList());
		
		return collect;
	}
}